package com.winfield.service;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.winfield.photovideo.R.string;
import com.winfield.utils.FormFile;
import com.winfield.utils.MyNetWork;
import com.winfield.utils.SocketHttpRequester;

/**
 * 
 * 业务bean  用于保存数据
 * @author Winfield
 * 
 *
 */
public class NewsService {


	
	
	public static boolean save(String userID,String pvDescribe, String pvAddress,
			String pvTime) {
		//String path = "http://172.16.44.204:8080/PvAppWeb/ManagerServlet";
		//String path = "http://172.18.92.69:8080/PvAppWeb/ManagerServlet" ;
		String path = MyNetWork.PVAPPWEB_J+"/ManagerServlet" ;
		Map<String, String> params = new HashMap<String, String>();
		params.put("userID", userID);
		params.put("pvDescribe", pvDescribe);
		params.put("pvAddress", pvAddress);
		params.put("pvTime", pvTime);
		try {
			
			return sendHttpClientPOSTRequest(path,params,"UTF-8");
		}catch (Exception e) {

			e.printStackTrace();
		}
		
		return false;
	}

	/**
	 *该方法用的是Android自带的框架   post主要思路  文件头+实体数据 
	 * @param path
	 * @param params
	 * @param myEnCode
	 * @return
	 * @throws Exception 
	 */
	private static boolean sendHttpClientPOSTRequest(String path,
			Map<String, String> params, String myEnCode) throws Exception {

		List<NameValuePair> pairs = new ArrayList<NameValuePair>();//存放请求参数 NameValuePair是接口
		if(params!=null&&!params.isEmpty()){
			for(Map.Entry<String, String> entry:params.entrySet()){
				//BasicNameValuePair继承了NameValuePairNameValuePair
				//此处采用了两个父类采用子类对象  如list与子类ArrayList  接口NameValuePair  而放在数组中 最后是放入BasicNameValuePair
				pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				//new BasicNameValuePair(entry.getKey(), entry.getValue());
			}
		}
		//构造实体
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, myEnCode);
		HttpPost httppost = new HttpPost(path);//发送的地址
		httppost.setEntity(entity);
		DefaultHttpClient client = new DefaultHttpClient();
		HttpResponse response = client.execute(httppost);
		if(response.getStatusLine().getStatusCode() == 200){
			return true;
		}
		return false;
		
	}




	public static boolean save(String userID,String pvDescribe, String pvAddress,
			String pvTime, File uploadFile) {
		Log.e("FileSize", String.format(new Long(uploadFile.length()/1024).toString()));
		//String path = "http://172.16.44.204:8080/PvAppWeb/ManagerServlet";
		//String path = "http://172.18.92.69:8080/PvAppWeb/ManagerServlet";
		String path = MyNetWork.PVAPPWEB_J+"/ManagerServlet" ;
		Map<String, String> params = new HashMap<String, String>();
		params.put("userID", userID);
		params.put("pvDescribe", pvDescribe);
		params.put("pvAddress", pvAddress);
		params.put("pvTime", pvTime);
		
		try {
			//TODO
			String contentType =ContentTypeDictionary.getType(uploadFile.getName());
			if(contentType==null){
				//在字典中没有找到对应的类型 这个时候不应该去存储  
				Log.e("wError","字典中没有匹配的类型");
				return false;
			}
			FormFile formFile = new FormFile(uploadFile, "videofile",contentType);
			return SocketHttpRequester.post(path, params, formFile);
		}catch (Exception e) {

			e.printStackTrace();
		}
		return false;
	}

	
	
	

	
}
